package com.zhouyu.exampleThread.theadLocalTest;

import com.zhouyu.exampleThread.builder.ThreadPoolBuilder;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * Description:测试threadlocal
 *
 * @author zhouyu
 * @create 2020/7/21
 */
public class MyThreadLocalTest {

    private static ThreadLocal<Long> threadLocal = new ThreadLocal();

    public static void set() {
        threadLocal.set(1L);
    }

    public static long get() {
        return threadLocal.get();
    }

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            set();
            System.out.println(get());
        }).start();
        // 目的就是为了让子线程先运行完
        Thread.sleep(100);
        System.out.println(get());
    }

    public void testThreadLocal() {
        for (int i = 0; i < 10; i++) {
            ThreadPoolTaskExecutor threadPoolTaskExecutor = ThreadPoolBuilder.builCommonThreadPool();
            threadPoolTaskExecutor.submit(() -> {
                String name = Thread.currentThread().getName();
                ThreadLocal<String> threadLocal = new ThreadLocal<>();
                threadLocal.set(name + "信号量");
            });

        }

    }

}